package com.situ.hospital.servlet;

import com.situ.hospital.pojo.Order;
import com.situ.hospital.pojo.Patient;
import com.situ.hospital.pojo.User;
import com.situ.hospital.pojo.query.OrderQuery;
import com.situ.hospital.pojo.vo.InventoryVO;
import com.situ.hospital.pojo.vo.OrderVO;
import com.situ.hospital.service.IMedicineService;
import com.situ.hospital.service.IOrderService;
import com.situ.hospital.service.IPatientService;
import com.situ.hospital.service.IUserService;
import com.situ.hospital.service.impl.MedicineServiceImpl;
import com.situ.hospital.service.impl.OrderServiceImpl;
import com.situ.hospital.service.impl.PatientServiceImpl;
import com.situ.hospital.service.impl.UserServiceImpl;
import com.situ.hospital.util.JSONUtil;
import com.situ.hospital.util.MyBeanUtil;
import com.situ.hospital.util.PageResult;
import com.situ.hospital.util.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/order")
public class OrderServlet extends HttpServlet {

    private IOrderService orderService = new OrderServiceImpl();
    private IUserService userService = new UserServiceImpl();
    private IPatientService patientService = new PatientServiceImpl();
    private IMedicineService medicineService = new MedicineServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        switch (method) {
            case "selectByPage":
                selectByPage(req,resp);
                break;
            case "deleteById":
                deleteById(req,resp);
                break;
            case "deleteAll":
                deleteAll(req,resp);
                break;
            case "add":
                add(req,resp);
                break;
            case "selectMap":
                selectMap(req,resp);
                break;
            case "update":
                update(req,resp);
                break;
        }
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("OrderServlet.update");
        Order order = MyBeanUtil.copyToBean(req, Order.class);
        orderService.update(order);
        JSONUtil.toJSON(resp, Result.ok("更新成功"));
    }

    private void selectMap(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("OrderServlet.selectMap");
        String id = req.getParameter("id");
        Order order = orderService.selectById(Integer.parseInt(id));
        List<User> userList = userService.selectUser();
        List<Patient> patientList = patientService.selectAll();
        List<InventoryVO> inventoryList = medicineService.selectQuantity(order.getMedicineId());
        Map<String,Object> map = new HashMap<>();
        map.put("order",order);
        map.put("userList",userList);
        map.put("patientList",patientList);
        map.put("inventoryList",inventoryList);
        JSONUtil.toJSON(resp, Result.ok(map));
    }

    private void add(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("OrderServlet.add");
        Order order = MyBeanUtil.copyToBean(req, Order.class);
        orderService.add(order);
        JSONUtil.toJSON(resp, Result.ok("添加成功"));
    }

    private void deleteAll(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("OrderServlet.deleteAll");
        String[] ids = req.getParameterValues("ids[]");
        orderService.deleteAll(ids);
        JSONUtil.toJSON(resp, Result.ok("删除成功"));
    }

    private void deleteById(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("OrderServlet.deleteById");
        String id = req.getParameter("id");
        orderService.deleteById(Integer.parseInt(id));
        JSONUtil.toJSON(resp, Result.ok("删除成功"));
    }

    private void selectByPage(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("OrderServlet.selectByPage");
        OrderQuery orderQuery = MyBeanUtil.copyToBean(req, OrderQuery.class);
        PageResult<OrderVO> pageResult = orderService.selectByPage(orderQuery);
        JSONUtil.toJSON(resp, pageResult);
    }
}
